package com.um.jdy.app.charge.scheduled;

import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.charge.manager.EBOrderManager;
import com.um.jdy.app.charge.manager.EBoxOrderManager;
import com.um.jdy.app.device.manager.EBDeviceManager;
import com.um.jdy.app.device.manager.EBoxDeviceManager;
import com.um.jdy.common.charge.entity.po.Order;
import com.um.jdy.common.charge.enums.OrderEnum;
import com.um.jdy.common.charge.enums.OrderStopReason;
import com.um.jdy.common.charge.service.OrderService;
import com.um.jdy.common.member.enums.PayEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
@Slf4j
@Component
public class EBoxOrderTask {
    @Autowired
    OrderService orderService;

    @Autowired
    EBoxOrderManager eBoxOrderManager;

    @Autowired
    EBoxDeviceManager eBoxDeviceManager;

    /**
     * 订单超时未启动处理任务（一次性定时任务）
     *
     * @param orderNo
     */
    @Async
    public void timeoutNotStartedTask(String orderNo) {
        Order order = orderService.getRow(new String[]{"order_no", "status"},
                new Object[]{orderNo, OrderEnum.Status.NotStarted.name()});
        if (null == order) {
            return;
        }

        log.info("自动取消未启动订单{}", orderNo);
        try {
            eBoxOrderManager.cancelPaidOrder(orderNo, OrderStopReason.EBox.PLATFORM_CANCEL.name(), true);
        } catch (Exception ex) {
            log.error(StrUtil.format("自动取消未启动订单{}出错", orderNo), ex);
        }
    }

//    /**
//     * 订单15秒未成功，添加语音播报放：请插入插头（一次性定时任务）
//     *
//     * @param orderNo
//     */
//    @Async
//    public void timeoutPlayTTSTask(String orderNo) {
//        Order order = orderService.getRow(new String[]{"order_no", "pay_status"},
//                new Object[]{orderNo, PayEnum.Status.UnPayed.name()});
//        if (null == order) {
//            return;
//        }
//
//        log.info("开始提示用户插上插头{}", orderNo);
//        try {
//            eBoxDeviceManager.sendTTS(order.getDeviceCode(),"订单未启动充电，请检查充电器插头是否已插上");
//        } catch (Exception ex) {
//            log.error(StrUtil.format("开始提示用户插上插头{}出错", orderNo), ex);
//        }
//    }
}
